<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage="/error.jsp"%>
<%@page import="dbexp.model.View"%>
<%@page import="dbexp.model.Column"%>
<%@page import="dbexp.model.db.DbUser"%>
<%@page import="dbexp.connection.DbMetaData"%>
<%@page import="org.json.JSONObject"%>
<%@page import="org.json.JSONArray"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%
DbUser u = (DbUser)session.getAttribute("USER");	//pobieramy uzytkownika z sesji
String name = request.getParameter("name");				//pobieramy nazwe widoku z zadania
String schema = request.getParameter("schema");		//pobieramy nazwe schematu z zadania

boolean nowy = (name==null || name.trim().equals("null")) && (schema==null || schema.trim().equals("null"));
String id = schema+"-"+name;											///ustawiamy id dla kontrolek
View object = null;
	JSONArray reader_jsoncs = new JSONArray();
	JSONArray grid_jsoncs = new JSONArray();
if(!nowy){
	object = DbMetaData.getView(u, name.toUpperCase(), schema.toUpperCase());	//czytamy podstawowe dane widoku
	
	List<Column> columns = DbMetaData.getColumnsFromQuery(u, "SELECT * FROM "+object.getObjectNameForQuery() + " WHERE ROWNUM<=1");	//czytamy liste kolumn z widoku
	reader_jsoncs.put( new JSONObject().put("name","NR"));
	JSONObject grid_jsonc = new JSONObject();
	grid_jsonc.put("header", "NR");
	grid_jsonc.put("width",20);
	grid_jsonc.put("sortable",true);
	grid_jsoncs.put(grid_jsonc);
	for (Column column : columns ) {			//czytamy wszystkie kolumny
		JSONObject reader_jsonc = new JSONObject();
		JSONObject grid_prop = new JSONObject();
		
		reader_jsonc.put("name", column.getName().toLowerCase());
		reader_jsoncs.put(reader_jsonc);
		
		grid_prop.put("header", column.getName().toLowerCase());
		grid_prop.put("width",50);
		grid_prop.put("sortable",true);
		grid_jsoncs.put(grid_prop);
	}
}
%>
<div class="container">
    <div id="<%=id%>-view-data-grid"></div>
</div>
<script language="JavaScript">

Ext.onReady(function() {
	try{
	var store = new Ext.data.JsonStore({
        remoteSort: true,
        baseParams: {name:'<%=name %>', schema:'<%=schema %>'},
        autoLoad: {params:{start:0, limit:50}},
		totalProperty: 'total',
		successProperty: 'success',
		idProperty: 'NR',
		root: 'data',

        proxy: new Ext.data.HttpProxy({
            url: 'db/view/view-data.jsp'
        }),

        fields: <%=reader_jsoncs.toString()%>
    });
 var grid = new Ext.grid.GridPanel({
	 renderTo:"<%=id%>-view-data-grid",
     border:false,
     height:350,
     frame:true,
     layout:'fit',
     trackMouseOver:false,
     store: store,
     viewConfig:{forceFit:true},
     columns: <%=grid_jsoncs.toString()%>,
	    bbar: new Ext.PagingToolbar({
		    store: store,
		    pageSize:50,
		    displayInfo:true
	    })//,

//	    view: new Ext.ux.grid.BufferView({
		    // custom row height
//		    rowHeight: 34,
		    // render rows as they come into viewable area.
//		    scrollDelay: false
//	    })
 });

	}catch(e){alert(e);}
});
</script>